﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BigMonitoringWarningApi.Tool.Extension
{
    public static partial class GyctExtention
    {
        /// <summary>
        /// HTML转行成TEXT
        /// </summary>
        /// <param name="htmlStr">html字符串</param>
        /// <returns></returns>
        public static string HtmlToTxt(this string htmlStr)
        {
            if (string.IsNullOrEmpty(htmlStr)) return htmlStr;
            string[] aryReg ={
                @"<script[^>]*?>.*?</script>",
                @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
                @"([\r\n])[\s]+",
                @"&(quot|#34);",
                @"&(amp|#38);",
                @"&(lt|#60);",
                @"&(gt|#62);",
                @"&(nbsp|#160);",
                @"&(iexcl|#161);",
                @"&(cent|#162);",
                @"&(pound|#163);",
                @"&(copy|#169);",
                @"&#(\d+);",
                @"-->",
                @"<!--.*\n"
            };
            var strOutput = aryReg.Select(item => new Regex(item, RegexOptions.IgnoreCase)).Aggregate(htmlStr, (current, regex) => regex.Replace(current, ""));
            strOutput = strOutput.Replace("<", "");
            strOutput = strOutput.Replace(">", "");
            strOutput = strOutput.Replace(" ", "");
            strOutput = strOutput.Replace("　", "");
            strOutput = strOutput.Replace("\r\n", "");
            return strOutput;
        }
    }
}
